package com.cyys.modules.system.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyys.common.annotation.SystemLog;
import com.cyys.common.ql.AR;
import com.cyys.common.ql.JSONData;
import com.cyys.common.validate.Detail;
import com.cyys.common.validate.Edit;
import com.cyys.common.validate.Insert;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.core.sysParam.ParamBasicUtil;
import com.cyys.modules.core.sysParam.ParamConstants;
import com.cyys.modules.system.model.user.Menu;
import com.cyys.modules.system.service.user.MenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Controller
@RequestMapping("/sys/menu")
@Validated
public class MenuAction extends BaseAction {

	@Resource
	private MenuService menuService;
	
	@GetMapping("/add/input")
	@RequiresPermissions("sys:menu:add")
	public String input(Menu menu, Model model)  {
		model.addAttribute("isDeveloper", isSystemDeveloper());
		Menu m = menuService.getById(menu.getParentId());
		if(m!=null){
			menu.setParentName(m.getName());
		}
		model.addAttribute("menu", menu);
		model.addAttribute("isShowMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.IS_SHOW));
		model.addAttribute("MODE_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.MENU_MODE));

		if(isSystemDeveloper()){
			model.addAttribute("typeMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.MENU_TYPE));
		}
		return "module/system/user/menu-input";
	}

	@ResponseBody
	@PostMapping("/add/save")
	@RequiresPermissions(value = "sys:menu:add")
	@SystemLog(module = "系统设置", methods = "权限菜单-保存权限菜单")
	public AR save(@Validated(Insert.class)Menu menu)  {
		return menuService.save(menu);
	}

	@GetMapping("/edit/input")
	@RequiresPermissions("sys:menu:edit")
	public String editInput(@Validated(Detail.class) Menu menu, Model model)  {
		model.addAttribute("isDeveloper", isSystemDeveloper());
		Menu m = menuService.getById(menu.getId());
		Menu pm = menuService.getById(menu.getParentId());
		if(pm!=null){
			m.setParentName(pm.getName());
		}
		model.addAttribute("menu", m);
		model.addAttribute("isShowMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.IS_SHOW));
		model.addAttribute("MODE_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.MENU_MODE));
		if(isSystemDeveloper()){
			model.addAttribute("typeMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.MENU_TYPE));
		}
		return "module/system/user/menu-input";
	}

	@ResponseBody
	@PostMapping("/edit/save")
	@RequiresPermissions(value = "sys:menu:edit")
	@SystemLog(module = "系统设置", methods = "权限菜单-更新权限菜单")
	public AR update(@Validated(Edit.class)Menu menu)  {
		return menuService.update(menu);
	}

	@GetMapping("/list")
	@RequiresPermissions("sys:menu:list")
	public String list(Model model) {
		return "module/system/user/menu-list";
	}

	@ResponseBody
	@PostMapping("/del/deleteById")
	@RequiresPermissions("sys:menu:del")
	@SystemLog(module = "系统设置", methods = "权限菜单-删除权限菜单")
	public AR deleteById(@NotBlank(message = "ID不能为空") String id)  {
		return menuService.deleteById(id);
	}

	@ResponseBody
	@RequiresPermissions("sys:menu:del")
	@PostMapping("/del/deleteByIds")
	@SystemLog(module = "系统设置", methods = "权限菜单-批量删除权限菜单")
	public AR deleteByIds(@NotBlank(message = "ID不能为空") String ids)  {
		return menuService.deleteByIds(ids);
	}

	@ResponseBody
	@PostMapping("/list/menuList")
	@RequiresPermissions("sys:menu:list")
	@SystemLog(module = "系统设置", methods = "权限菜单-查询权限菜单")
	public String menulist(Menu menu, Page page, Model model)  {
		if (menu != null && menu.getName() != null && menu.getName().equals("")) {
			menu.setName(null);
		}
		QueryWrapper<Menu> qw = new QueryWrapper(menu);
		qw.orderByAsc("parent_id").orderByAsc("sort");
		return JSONData.toLayerData(menuService.list(page , qw,false));
	}

	@ResponseBody
	@RequestMapping("/list/listAllTree")
	public String listAllTree(Menu menu, Boolean queryTop, Boolean extendAll)  {
		QueryWrapper qw= new QueryWrapper<>(menu);
		qw.orderByAsc("sort");
		List<Menu> orgTree = menuService.listAllTree(qw);
		String menuArray = "[";
		if (queryTop != null && queryTop) {
			menuArray += "{id:\"0\"," + "pId:\"\",name:\"顶级节点\"";
			menuArray += ",isParent:false"; //这里为true的话，前台顶级节点一直点一直有
			menuArray += ",dropRoot:false";
			menuArray += ",childOuter:false";
			menuArray += ",open:true},";
		}
		menuArray += getMenuArray(orgTree, extendAll);
		int indx = menuArray.lastIndexOf(",");
		if (indx != -1)
			menuArray = menuArray.substring(0, indx);
		menuArray += "]";
		return menuArray;
	}

//	@ResponseBody
//	@PostMapping("sys:menu:list")
//	@RequestMapping("/list/getByParentId")
//	public String getByParentId(String parentId, Boolean queryTop)  {
//		if (queryTop == null) { // 是否查询顶级节点
//			queryTop = true;
//		}
//		List<Menu> menuList = menuService.listByParentId(parentId, isSystemDeveloper(),true);
//		String menuArray = "[";
//		if (queryTop) {
//			menuArray += "{id:\"0\"," + "pId:\"\",name:\"顶级节点\"";
//			menuArray += ",isParent:false";
//			menuArray += ",dropRoot:false";
//			menuArray += ",childOuter:false";
//			menuArray += ",open:true},";
//		}
//		menuArray += getMenuArray(menuList);
//		int indx = menuArray.lastIndexOf(",");
//		if (indx != -1)
//			menuArray = menuArray.substring(0, indx);
//		menuArray += "]";
//		return menuArray;
//	}

	private String getMenuArray(List<?> list, Boolean extendAll) {
		String menuArray = "";
		for (int i = 0; i < list.size(); i++) {
			Menu t = (Menu) list.get(i);
			menuArray += "{id:\"" + t.getId() + "\"," + "pId:\"" + t.getParentId() + "\",name:\"" + t.getName() + "\"";
			if (t.getHasChild() == 1) {
				menuArray += ",isParent:true";
				menuArray += ",dropRoot:false";
				menuArray += ",open:true";
				menuArray += ",childOuter:false";
			} else {
				menuArray += ",dropInner:false,childOuter:false";
			}
			if (t.getChildren() != null && t.getChildren().size() > 0) {
				if((extendAll !=null && extendAll) || (t.getIsRoot() != null && t.getIsRoot()))
					menuArray += ",open:true},";
				else
					menuArray += "},";
				menuArray += getMenuArray(t.getChildren(),extendAll);
			} else {
				menuArray += "},";
			}
		}
		return menuArray;
	}

	@GetMapping("setMenu")
	public String setMenu(Model model, Boolean queryTop) {
		model.addAttribute("queryTop", queryTop);
		return "module/system/user/set-menu";
	}

}
